﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using CastleDefender.Platform.ScreenBehavior;
using CastleDefender.ConfigFolder;
using CastleDefender.Object;
using Microsoft.Xna.Framework.Media;

namespace CastleDefender.Platform.GameScene
{
    public partial class GameMenu : GameScene
    {
        #region VARIABLE

        protected int intChoose;
        protected int intCurrentChoose;
        protected bool blnActivated;
        protected bool blnChoose;
        protected TouchBehavior tbTouch;
        protected Animation2D aniChoose;
        protected Button btnNewGame;
        protected Button btnHighScore;
        protected Button btnCredits;
        protected Button btnHelp;
        protected Button btnQuit;


        #endregion

        #region CONSTRUCTOR

        public GameMenu(Game myGame, ref TouchBehavior touch)
            : base(myGame, Config.FolderBackgrounds + "Menu")
        {
            //Load hình ảnh
            tbTouch = touch;
            intChoose = -1;
            intCurrentChoose = -1;
            blnActivated = false;

            #region Texture Menu

            //Choose
            Texture2D textureChoose = myGame.Content.Load<Texture2D>(Config.FolderObject + Config.FolderMenus + "Choose");

            //New Game
            Texture2D textureNewGame = myGame.Content.Load<Texture2D>(Config.FolderObject + Config.FolderMenus + "NewGame");

            //HeightScore 
            Texture2D textureHeightScore = myGame.Content.Load<Texture2D>(Config.FolderObject + Config.FolderMenus + "HighScore");

            //Credits
            Texture2D textureCredits = myGame.Content.Load<Texture2D>(Config.FolderObject + Config.FolderMenus + "Credits");

            //Help
            Texture2D textureHelp = myGame.Content.Load<Texture2D>(Config.FolderObject + Config.FolderMenus + "Help");

            //Quit
            Texture2D textureQuit = myGame.Content.Load<Texture2D>(Config.FolderObject + Config.FolderMenus + "Quit");

            #endregion

            //Khởi tạo các đối tượng 
            //Lấy tọa độ y gốc
            btnNewGame = new Button(myGame, ref textureNewGame, textureNewGame.Width, 50, false, true,
                new Vector2((Config.ScreenSize.X - textureNewGame.Width) / 2 + Config.MenuMarginX, Config.MenuPosYInit));

            //Tăng tọa độ y theo btnContinue
            btnHighScore = new Button(myGame, ref textureHeightScore, textureHeightScore.Width, 50, false, true,
                new Vector2((Config.ScreenSize.X - textureHeightScore.Width) / 2 + Config.MenuMarginX, btnNewGame.Position.Y + btnNewGame.HeightFrame + Config.MenuMarginY));

            //Tăng tọa độ y theo btnHeightScore
            btnCredits = new Button(myGame, ref textureCredits, textureCredits.Width, 50, false, true,
                new Vector2((Config.ScreenSize.X - textureCredits.Width) / 2 + Config.MenuMarginX, btnHighScore.Position.Y + btnHighScore.HeightFrame + Config.MenuMarginY));

            //Tăng tọa độ y theo btnCreadits
            btnHelp = new Button(myGame, ref textureHelp, textureHelp.Width, 50, false, true,
                new Vector2((Config.ScreenSize.X - textureHelp.Width) / 2 + Config.MenuMarginX, btnCredits.Position.Y + btnCredits.HeightFrame + Config.MenuMarginY));

            //Tăng tọa độ y theo btnHelp
            btnQuit = new Button(myGame, ref textureQuit, textureQuit.Width, 50, false, true,
                new Vector2((Config.ScreenSize.X - textureQuit.Width) / 2 + Config.MenuMarginX, btnHelp.Position.Y + btnHelp.HeightFrame + Config.MenuMarginY));

            aniChoose = new Animation2D(myGame, ref textureChoose, 45, 45, false, true,
                new Vector2((int)btnNewGame.Position.X - 45 - 10, (int)btnNewGame.Position.Y), 100);


            //Đưa các đối tượng vào danh sách component
            lstComponents.Add(btnNewGame);
            lstComponents.Add(btnHighScore);
            lstComponents.Add(btnCredits);
            lstComponents.Add(btnHelp);
            lstComponents.Add(btnQuit);
            lstComponents.Add(aniChoose);

            SetOffButtons();
            btnNewGame.Active = true;
        }

        #endregion

        #region PUBLIC METHOD

        public override void Show()
        {
            intChoose = -1;
            MediaPlayer.Play(audio.Menu);
            MediaPlayer.IsRepeating = true;
            base.Show();
        }

        public override void Hide()
        {
            base.Hide();
        }

        public override void Update(GameTime gameTime)
        {
            if (Visible && Enabled)
            {
                //Cập nhật thay đổi của menu
                UpdateMenu();
                base.Update(gameTime);
            }
        }

        #endregion

        #region PRIVATE METHOD

        //Cập nhật lựa chọn menu
        private void UpdateMenu()
        {
            //Cập nhật menu khi chạm vào menu
            if ((tbTouch.IsPressed || tbTouch.IsMoved))
            {
                //Tọa độ con trỏ
                 Point pos = new Point((int)tbTouch.PosCurrent.X, (int)tbTouch.PosCurrent.Y);

                //Nhận tọa độ điểm chạm
                btnNewGame.GetTouchPosition(pos);
                btnHighScore.GetTouchPosition(pos);
                btnCredits.GetTouchPosition(pos);
                btnHelp.GetTouchPosition(pos);
                btnQuit.GetTouchPosition(pos);
                intCurrentChoose = -1;

                //Khi một trong các nút bấm được kích hoạt
                blnActivated = (btnNewGame.Touch || btnHighScore.Touch || btnCredits.Touch || btnHelp.Touch || btnQuit.Touch);

                //Tắt tất hiệu ứng tất cả các nút bấm
                if (blnActivated)
                {
                    SetOffButtons();
                }

                //NewGame
                if (btnNewGame.Touch)
                {
                    intCurrentChoose = 0;
                    btnNewGame.Active = true;
                    aniChoose.Position = new Vector2((int)btnNewGame.Position.X - aniChoose.WidthFrame - 10, (int)btnNewGame.Position.Y);
                }

                //HeightScore
                if (btnHighScore.Touch)
                {
                    intCurrentChoose = 1;
                    btnHighScore.Active = true;
                    aniChoose.Position = new Vector2((int)btnHighScore.Position.X - aniChoose.WidthFrame - 10, (int)btnHighScore.Position.Y);
                }

                //Credits
                if (btnCredits.Touch)
                {
                    intCurrentChoose = 2;
                    btnCredits.Active = true;
                    aniChoose.Position = new Vector2((int)btnCredits.Position.X - aniChoose.WidthFrame - 10, (int)btnCredits.Position.Y);
                }

                //Help
                if (btnHelp.Touch)
                {
                    intCurrentChoose = 3;
                    btnHelp.Active = true;
                    aniChoose.Position = new Vector2((int)btnHelp.Position.X - aniChoose.WidthFrame - 10, (int)btnHelp.Position.Y);
                }

                //Quit 
                if (btnQuit.Touch)
                {
                    intCurrentChoose = 4;
                    btnQuit.Active = true;
                    aniChoose.Position = new Vector2((int)btnQuit.Position.X - aniChoose.WidthFrame - 10, (int)btnQuit.Position.Y);
                }

            }

            //Thực hiện lựa chọn khi một trong các nút bấm được kích hoạt thực sự 
            if (tbTouch.IsKeyUp && blnActivated)
            {
                intChoose = intCurrentChoose;
                blnActivated = false;
            }


        }

        //Ngừng hoạt động tất cả các nút bấm
        private void SetOffButtons()
        {
            btnNewGame.Active = false;
            btnHighScore.Active = false;
            btnCredits.Active = false;
            btnHelp.Active = false;
            btnQuit.Active = false;
        }

        #endregion

        #region PROPERTIES

        public int SelectedIndex
        {
            get { return intChoose; }
            private set { }
        }
        #endregion
    }
}
